﻿using Newtonsoft.Json;
using Region.Spider;
using Region.Spider.aoshang;

var regionFile = "output/region.json";
var regionTownFile = "output/region_town.json";
//var aoshangFile = "output/aoshang/area.json";
//var aoshangOldFile = "output/aoshang/area_extra.json";

var spider = new AreaSpider();

IList<AreaItem> items;
IList<TownAreaItem> townItems;

if (File.Exists(regionFile) && File.Exists(regionTownFile))
{
    items = JsonConvert.DeserializeObject<IList<AreaItem>>(await File.ReadAllTextAsync(regionFile));
    townItems = JsonConvert.DeserializeObject<IList<TownAreaItem>>(await File.ReadAllTextAsync(regionTownFile));
}
else
{
    var spiderResult = await spider.DoAsync();
    items = spiderResult.Item1;
    townItems = spiderResult.Item2;
    await Tools.GenerateJsonFileAsync(items, regionFile);
    await Tools.GenerateJsonFileAsync(townItems, regionTownFile);
}

//生成aoshang需要的数据格式
await AoshangTools.GenerateJsonFileAsync(items);

Console.WriteLine("数据生成完成，采集结束..");

Console.ReadLine();
await spider.DisposeAsync();